package com.example.demo.controller;

import com.example.demo.common.DataResult;
import com.example.demo.utils.PhoneCode;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

/**
 * @ClassName: LoginController
 * TODO:写注释
 * @Author: zjf
 * @CreateDate: 2020-11-03 16:26
 * @Version: 0.0.1
 */
@RestController
public class LoginController {
    @Autowired
    private Producer kaptchaProducer;

    @PostMapping(value = "/login")
    public DataResult login(@RequestParam(value = "username") String username,
                            @RequestParam(value = "password")  String password,
                            @RequestParam(value = "code") String code,
                            @RequestParam(value = "verCode") String verCode, HttpServletRequest request){
        DataResult result = null;
        String captchaId =  request.getSession().getAttribute("rightCode").toString().toLowerCase();
        request.getSession().removeAttribute("rightCode");
        if (StringUtils.isEmpty(captchaId.toLowerCase()) || !captchaId.equals(code.toLowerCase())){
            result = DataResult.failure("验证码错误");
            return result;
        }
        /**
         * 获取短信验证码
         */
        String xyz = PhoneCode.getPhoneMsg(verCode);
        System.out.println(xyz);

        if (username.equals("a") && password.equals("a")){
            result = DataResult.success();
            return result;
        }else{
            result = DataResult.failure("登录失败，用户名或密码不对！");
            return result;
        }
    }

    @GetMapping(value = "/kaptcha")
    public void getKaptcha(HttpServletRequest request,HttpServletResponse response) throws Exception{
        byte[] captcha = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            // 将生成的验证码保存在session中
            String createText = this.kaptchaProducer.createText();
            System.out.println(createText);
            request.getSession().setAttribute("rightCode", createText);
            BufferedImage bi = this.kaptchaProducer.createImage(createText);
            System.out.println(bi);
            ImageIO.write(bi, "jpg", out);
        } catch (Exception e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        captcha = out.toByteArray();
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        ServletOutputStream sout = response.getOutputStream();
        sout.write(captcha);
        sout.flush();
        sout.close();

    }

    @GetMapping(value = "/getPhone")
    public String getPhoneCode(String phone){
        return PhoneCode.getPhoneMsg(phone);
    }

}
